/******************************************************************************
** Name: rbuff.cpp                                                           **
** Description: A ring dual, multi-level ring buffer for storing data        **
**              from USART, network, SPI, etc.                               **
**                                                                           **
** Open Source Initiative (OSI) Approved License                             **
**                                                                           **
**  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
**  All rights reserved.                                                     **
**                                                                           **
**  Redistribution and use in source and binary forms, with or without       **
**  modification, are permitted provided that the following conditions are   **
**  met:                                                                     **
**                                                                           **
**  * Redistributions of source code must retain the above copyright notice, **
**    this list of conditions and the following disclaimer.                  **
**  * Redistributions in binary form must reproduce the above copyright      **
**    notice, this list of conditions and the following disclaimer in the    **
**    documentation and/or other materials provided with the distribution.   **
**                                                                           **
**  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
**                                                                           **
******************************************************************************/
// $Id: rbuff.cpp 7 2010-07-14 12:58:31Z nevdull69 $

// $Id: rbuff.cpp 7 2010-07-14 12:58:31Z nevdull69 $

#include "rbuff.h"
#include <string.h>

void
RingBuff::CopyBuff(char * dest) const
{
	memcpy(dest, (char*)_buff, DEFAULT_RING_BUFF_SZ);
}


// Push onto the tail
void
RingBuff::Put(unsigned char c) volatile
{
	_buff[_tail++] = c;
	if (_tail >= _sz) _tail = 0;
	++_len;
}

// Pop from the head
unsigned char
RingBuff::Get() volatile
{
	if (_len == 0)
		return 0;

	unsigned char c = _buff[_head++];
	if (_head >= _sz) _head = 0;
	--_len;

	return c;
}

RingBuff::RingBuff(const RingBuff& rb)
{
    delete [] _buff;
    _buff = new volatile unsigned char[rb.Size()];
    rb.CopyBuff((char*)_buff);
    _head = rb.Head();
    _tail = rb.Tail();
    _sz   = rb.Size();
}

RingBuff::RingBuff(uint8 sz)
{
    _sz = sz;
    _buff = new volatile unsigned char[_sz];
	_head = _tail = _len = 0;
}
